import { _decorator, Button, Component, Label, Node } from 'cc';
import EventMgr from '../../new/event/EventMgr';
import { EventId } from '../../new/event/EventId';
import { BattleRole } from '../../new/role/BattleRole';
import { BattleManager } from '../../new/BattleManager';
const { ccclass, property } = _decorator;

@ccclass('SkillPanel')
export class SkillPanel extends Component {
    @property(Node)
    btnAtk: Node;
    @property(Node)
    btnSkill1: Node;
    @property(Node)
    btnSkill2: Node;
    @property(Node)
    btnFree: Node;

    private curRole: BattleRole

    protected onLoad(): void {
    }

    start() {
        this.btnAtk.on(Button.EventType.CLICK, this.commonAtk, this)
        this.btnSkill1.on(Button.EventType.CLICK, this.selectSkill1, this)
        this.btnSkill2.on(Button.EventType.CLICK, this.selectSkill2, this)
        this.btnFree.on(Button.EventType.CLICK, this.doFree, this)
    }

    protected onEnable(): void {
        this.curRole = BattleManager.ins.curRole;
        if (!this.curRole) return;
        let skillDatas = this.curRole.skillMgr.skillDatas
        this.btnAtk.getChildByName("txtName").getComponent(Label).string = skillDatas[0].skillName
        this.btnSkill1.getChildByName("txtName").getComponent(Label).string = skillDatas[1].skillName
        this.btnSkill2.getChildByName("txtName").getComponent(Label).string = skillDatas[2].skillName
        this.btnFree.getChildByName("txtName").getComponent(Label).string = skillDatas[3].skillName
    }

    commonAtk(): void {
        this.selectSkill(0)
    }

    selectSkill1(): void {
        this.selectSkill(1)
    }

    selectSkill2(): void {
        this.selectSkill(2)
    }

    doFree(): void {
        this.selectSkill(3)
    }

    selectSkill(idx: number) {
        let skill = this.curRole.skillMgr.skillDatas[idx]
        EventMgr.ins.event(EventId.SELECT_SKILL, skill);
    }

    update(deltaTime: number) {

    }
}


